package com.example.myapplication1;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;

public class MyListActivity extends ListActivity {
    private static final String TAG = "MyListActivity";
    private SimpleAdapter listItemAdapter;
    private Handler handler;
    private ArrayList<HashMap<String, String>> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initListView();
        setListAdapter(listItemAdapter);
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, String> item = listItems.get(position);
                String currencyName = item.get("ItemTitle");
                String price = item.get("price");

                // 启动计算Activity并传递数据
                Intent intent = new Intent(MyListActivity.this, CalculateActivity2.class);
                intent.putExtra("currencyName", currencyName);
                intent.putExtra("price", price);
                startActivity(intent);
            }
        });
        handler = new Handler(Looper.myLooper()){
            public void handleMessage(@NonNull Message msg){
                if(msg.what==9){

                    listItems=(ArrayList<HashMap<String,String>>)msg.obj;
                    //              listItemAdapter = new SimpleAdapter(
                    //                  MyListActivity.this,
                    //                  listItems,
                    //               R.layout.list_item,
                    //               new String[]{"ItemTitle", "price"},
                    //               new int[]{R.id.itemTitle, R.id.price}
                    //       );
                    MyAdapter adapter=new MyAdapter(MyListActivity.this,R.layout.list_item,listItems);
                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
        //定义线程获取数据
        Thread t=new Thread(()->{
            ArrayList<HashMap<String,String>>list=new ArrayList<HashMap<String,String>>();
            try {
                Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
                Log.i(TAG, "run:title=" + doc.title());
                Elements tables = doc.getElementsByTag("table");
                Element table = tables.get(1);
                Log.i(TAG, "run:table2=" + table);
                Elements trs = table.getElementsByTag("tr");
                trs.remove(0);
                for (Element tr : trs) {
                    Elements tds = tr.children();
                    Element td1 = tds.first();
                    Element td2 = tds.get(5);
                    String str1 = td1.text();
                    String str2 = td2.text();
                    Log.i(TAG, "run:" + str1 + "==>" + str2);
                    float r = 100 / Float.parseFloat(str2);
                    HashMap<String, String> map = new HashMap<>();
                    map.put("ItemTitle",  str1);
                    map.put("price", str2);
                    list.add(map);
                }
            } catch (MalformedURLException e) {
                Log.e(TAG, "MalformedURLException: " + e.getMessage());
            } catch (IOException e) {
                Log.e(TAG, "IOException: " + e.getMessage());
            }
            Message msg = handler.obtainMessage(9, list);
            handler.sendMessage(msg);

        });
        t.start();
    }

    private void initListView() {
        listItems = new ArrayList<>();
        for (int j = 1; j < 10; j++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("ItemTitle", "Rate: " + j);
            map.put("price", "detail" + j);
            listItems.add(map);
        }

        listItemAdapter = new SimpleAdapter(
                this,
                listItems,
                R.layout.list_item,
                new String[]{"ItemTitle", "price"},
                new int[]{R.id.itemTitle, R.id.price}
        );
    }
}





